﻿Imports System.Data
Imports System.Data.SqlClient

Public Class frm_workHour


    Private Sub frm_workHour_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'load time from db
        Using connection As New SqlConnection(ConnectionString)
            connection.Open()
            Dim sql = "SELECT * FROM WorkHour"

            Dim cmd As New SqlCommand(sql, connection)
            cmd.CommandType = CommandType.Text

            Dim reader As SqlDataReader = cmd.ExecuteReader()
            If (reader.Read()) Then
                DateTimePicker1.Value = DateTime.ParseExact(reader.GetValue(1), "HH:mm", Nothing)
                DateTimePicker2.Value = DateTime.ParseExact(reader.GetValue(2), "HH:mm", Nothing)
                DateTimePicker3.Value = DateTime.ParseExact(reader.GetValue(3), "HH:mm", Nothing)
                DateTimePicker4.Value = DateTime.ParseExact(reader.GetValue(4), "HH:mm", Nothing)
                DateTimePicker5.Value = DateTime.ParseExact(reader.GetValue(5), "HH:mm", Nothing)
                DateTimePicker6.Value = DateTime.ParseExact(reader.GetValue(6), "HH:mm", Nothing)
                DateTimePicker7.Value = DateTime.ParseExact(reader.GetValue(7), "HH:mm", Nothing)
                DateTimePicker8.Value = DateTime.ParseExact(reader.GetValue(8), "HH:mm", Nothing)
                DateTimePicker9.Value = DateTime.ParseExact(reader.GetValue(9), "HH:mm", Nothing)
                DateTimePicker10.Value = DateTime.ParseExact(reader.GetValue(10), "HH:mm", Nothing)
                DateTimePicker11.Value = DateTime.ParseExact(reader.GetValue(11), "HH:mm", Nothing)
            End If
            reader.Close()
            cmd.Dispose()
        End Using
    End Sub

    Private Sub DateTimePicker1_KeyDown(sender As Object, e As KeyEventArgs) Handles DateTimePicker1.KeyDown, _
                DateTimePicker2.KeyDown, DateTimePicker3.KeyDown, DateTimePicker4.KeyDown, _
                DateTimePicker5.KeyDown, DateTimePicker6.KeyDown, DateTimePicker7.KeyDown, _
                DateTimePicker8.KeyDown, DateTimePicker9.KeyDown, DateTimePicker10.KeyDown, _
                DateTimePicker11.KeyDown

        Dim dtp As DateTimePicker = sender
        If e.KeyValue = 38 Then
            ' up
            dtp.Value = dtp.Value.AddMinutes(5)
            e.SuppressKeyPress = True
        ElseIf e.KeyValue = 40 Then
            'down
            dtp.Value = dtp.Value.AddMinutes(-5)
            e.SuppressKeyPress = True
        End If
    End Sub

    Private Sub btn_cancel_Click(sender As Object, e As EventArgs) Handles btn_cancel.Click
        Me.Close()
    End Sub

    Private Sub btn_update_Click(sender As Object, e As EventArgs) Handles btn_update.Click
        Using connection As New SqlConnection(ConnectionString)
            connection.Open()
            Dim sql = String.Format("UPDATE WorkHour SET MorStart='{0}'," & _
                                    "MorBreakStart = '{1}'," & _
                                    "MorBreakEnd = '{2}'," & _
                                    "LunchStart = '{3}'," & _
                                    "LunchEnd = '{4}'," & _
                                    "NoonStart = '{5}'," & _
                                    "NoonBreakStart = '{6}'," & _
                                    "NoonBreakEnd = '{7}'," & _
                                    "NoonEnd = '{8}'," & _
                                    "OTStart = '{9}'," & _
                                    "OTEnd = '{10}'", _
                                    DateTimePicker1.Value.ToString("HH:mm"), _
                                    DateTimePicker2.Value.ToString("HH:mm"), _
                                    DateTimePicker3.Value.ToString("HH:mm"), _
                                    DateTimePicker4.Value.ToString("HH:mm"), _
                                    DateTimePicker5.Value.ToString("HH:mm"), _
                                    DateTimePicker6.Value.ToString("HH:mm"), _
                                    DateTimePicker7.Value.ToString("HH:mm"), _
                                    DateTimePicker8.Value.ToString("HH:mm"), _
                                    DateTimePicker9.Value.ToString("HH:mm"), _
                                    DateTimePicker10.Value.ToString("HH:mm"), _
                                    DateTimePicker11.Value.ToString("HH:mm"))


            Dim cmd As New SqlCommand(sql, connection)
            cmd.CommandType = CommandType.Text

            Try
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error when update Working Hour")
            End Try
            cmd.Dispose()

            'SendUpdate()
            WorkingHourString = "WORK"
            WorkingHourString &= "|" & DateTimePicker1.Value.ToString("HH:mm")
            WorkingHourString &= "|" & DateTimePicker2.Value.ToString("HH:mm")
            WorkingHourString &= "|" & DateTimePicker3.Value.ToString("HH:mm")
            WorkingHourString &= "|" & DateTimePicker4.Value.ToString("HH:mm")
            WorkingHourString &= "|" & DateTimePicker5.Value.ToString("HH:mm")
            WorkingHourString &= "|" & DateTimePicker6.Value.ToString("HH:mm")
            WorkingHourString &= "|" & DateTimePicker7.Value.ToString("HH:mm")
            WorkingHourString &= "|" & DateTimePicker8.Value.ToString("HH:mm")
            WorkingHourString &= "|" & DateTimePicker9.Value.ToString("HH:mm")
            WorkingHourString &= "|" & DateTimePicker10.Value.ToString("HH:mm")
            WorkingHourString &= "|" & DateTimePicker11.Value.ToString("HH:mm")
            NeedToUpdateWorkHour = True
            MessageBox.Show("Update successfully")
            Me.Close()
        End Using

    End Sub

    Private Sub SendUpdate()
        Dim ip As String = "192.168.1.100"
        frm_host.myChat.SendData("WorkingHour", ip, 5000)
    End Sub

End Class